package com.ghost.view.pop.pack
{
	import flash.display.Sprite;
	
	import flashx.textLayout.elements.InlineGraphicElement;
	
	/**
	 * 背包具体物品存放
	 * @author ghost
	 */	
	public class Pack_content extends Sprite
	{
		public var max_w:Number;//最大宽度
		public var max_h:Number;//最大高度
		public var data_v:Vector.<Array>;//此背包数据 物品所在cell的序号 物品的id 物品的类型 物品的数量 物品的颜色序号 
		private var lock_num:int=49;//被lock的开始cell的序号(往后全部被锁) 0:全部没有开 49：全部开了
		private var cell_v_num:int=7;//cell的行数;
		private var cell_h_num:int=7;//cell的列数
		private var pack_cell:Pack_cell;//背包单元
		private var pack_cell_v:Vector.<Pack_cell>;//cell数组
		private var pack_cell_empty_v:Vector.<int>;//空的cell
		public var tab_num:int=-1;//标签序号
		public var empty_cell_num:int;//当前可用空cell的最小编号
		public function Pack_content()
		{
			pack_cell_v = new Vector.<Pack_cell>();
			pack_cell_empty_v = new Vector.<int>();
			data_v = new <Array>[new Array(0,'1',0,10,0),new Array(3,'2',0,10,1),new Array(4,'4',0,10,2),new Array(6,'4',0,10,2),new Array(10,'5',0,10,0)];
			lock_num = 15;
			super();
		}
		public function init(n:int):void
		{
			if(pack_cell==null)
			{
				var l_r_distance:int;
				var u_d_distance:int;
				var i:int = 0;
				for(;i<cell_v_num;i++)
				{
					for(var j:int = 0;j<cell_h_num;j++)
					{
						pack_cell = new Pack_cell();
						this.addChild(pack_cell);
						if(i==0&&j==0)
						{
							l_r_distance = int((max_w-cell_v_num*pack_cell.width)/cell_v_num);//左右间距
							u_d_distance = int((max_h-cell_h_num*pack_cell.height)/cell_h_num);//上下间距
						}
						pack_cell.x = j*(pack_cell.width+l_r_distance);
						pack_cell.y = i*(pack_cell.height+u_d_distance);
						pack_cell_v.push(pack_cell);
						pack_cell.name=String(n)+String(pack_cell_v.length-1);
						pack_cell_empty_v.push(pack_cell_v.length-1);
					}
				}
			}
		}
		/**
		 * 初始显示物品
		 * @param n标签序号
		 */		
		public function show(n:int):void
		{
			tab_num = n;
			var lock_v:Vector.<Pack_cell> = new Vector.<Pack_cell>();
			if(lock_num!=49 )
			{
				lock_v = pack_cell_v.slice(lock_num);
				pack_cell_empty_v.splice(lock_num,pack_cell_empty_v.length-lock_num);
			}
			var lock_l:int = lock_v.length;
			var k:int=0;
			for(;k<lock_l;k++)
			{
				lock_v[k].add('0',0);
			}
			var i:int=0;
			for(;i<lock_num;i++)
			{
				pack_cell_v[i].remove();
			}
			var data_l:int = data_v.length;
			var j:int=0;
			for(;j<data_l;j++)
			{
				pack_cell_v[data_v[j][0]].add(data_v[j][1],data_v[j][2],data_v[j][3],data_v[j][4]);
				pack_cell_v[data_v[j][0]].data_num = j;
				pack_cell_v[data_v[j][0]].cell_num = data_v[j][0];
				pack_cell_empty_v.splice(pack_cell_empty_v.indexOf(data_v[j][0]),1);
			}
			parse_pack_cell_empty_v(pack_cell_empty_v);
		}
		/**
		 * 增加物品
		 */		
		public function add(data_arr:Array):void
		{
			var i:int= 0;
			for(;i<data_v.length;i++)
			{
				if(data_arr[0]==data_v[i][1] && data_arr[3]==data_v[i][4])
				{
					
				}
			}
			pack_cell_v.push(data_arr);
			pack_cell_v[pack_cell_empty_v[0]].cell_num = pack_cell_empty_v[0];
			pack_cell_v[pack_cell_empty_v.shift()].add(data_arr[0],data_arr[1],data_arr[2],data_arr[3]);
		}
		/**
		 * 删除物品
		 */		
		public function remove():void
		{
			
		}
		public function parse_pack_cell_empty_v(pack_cell_empty_v:Vector.<int>):int
		{
			empty_cell_num = -1;
			if(pack_cell_empty_v.length!=0)
			{
				pack_cell_empty_v.sort(Array.NUMERIC);
				empty_cell_num = pack_cell_empty_v[0];
			}
			return empty_cell_num;
		}
	}
}